#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <QString>
#include <QFont>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->label->clear();
    ui->label->setAlignment(Qt::AlignCenter);
    ui->label->setFont(QFont("黑体", 20, QFont::Normal));

    QTimer* timer = new QTimer(this); // 定义QTimer对象
    connect(ui->start, &QPushButton::clicked, [=](){
        timer->start(1000); // 设置超时时间并启动定时，单位是 msec 毫秒
    });
    connect(timer, &QTimer::timeout, [=](){
        static int num = 1;
        ui->label->setText(QString::number(num++)); // QString::number 静态成员函数，将数值转为QString独享
    });
    connect(ui->end, &QPushButton::clicked, [=](){
        timer->stop(); // 停止定时任务
    });
}

Widget::~Widget()
{
    delete ui;
}

